require('/util/page')


const {formatTime,formatNumber}=require('./util/util')
const util=require('./util/util')

console.log(util)//对象结构：不需要使用访问时util.obj.a
//网站全局定义
let $https = require('./util/request')
wx.$https = $https 
wx.baseURL = $https.baseURL
wx.get = $https.get
wx.put = $https.put
wx.post = $https.post
wx.delete = $https.delete


// wx.$https=require('./util/request')

function Car(name,year){
  this.name = name
  this.year = year
  this.age = (x) => {
    return x - this.year
  }
}
Car.prototype.color = 'red'
console.log(new Car('Jeep',2014))


try{
  jgkj
  console.log(1)
}catch(e){
  console.log(e)
}finally{
  console.log(2)
}

var obj1 = {a:1,b:2,c:{f:1}}

//JSON文本转换为JS对象
console.log(JSON.parse('{"a":1,"b":2,"c":{"f":1}}'))

//JS对象转换为JSON文本
// console.log(JSON.stringify(obj1))

const config = require('./config')
const themeListeners = []
global.isDemo = true
App({
  
  onLaunch(opts, data) {
    //调用获取login
    wx.login({
      success :res =>{
        console.log('I am wx.login',res)
        wx.get('http://localhost:3000/wxlogin?code=' + res.code).then(res =>{
          console.log(res)
          wx.userInfo = res///存在wx的全局变量里面
          wx.token = res.token
          if(wx.waitLoginpages.length) {
            for(let i = 0; i < wx.waitLoginpages.length; i ++) {
              let page = wx.waitLoginpages[i]
              page.onLoad.call(page.context,page.options)
            }
          }
        })
      }
    }),
    // const that = this;
    // const canIUseSetBackgroundFetchToken = wx.canIUse('setBackgroundFetchToken')
    // if (canIUseSetBackgroundFetchToken) {
    //   wx.setBackgroundFetchToken({
    //     token: 'getBackgroundFetchToken',
    //   })
    // }
    // if (wx.getBackgroundFetchData) {
    //   wx.getBackgroundFetchData({
    //     fetchType: 'pre',
    //     success(res) {
    //       that.globalData.backgroundFetchData  = res;
    //       console.log('读取预拉取数据成功')
    //     },
    //     fail() {
    //       console.log('读取预拉取数据失败')
    //       wx.showToast({
    //         title: '无缓存数据',
    //         icon: 'none'
    //       })
    //     },
    //     complete() {
    //       console.log('结束读取')
    //     }
    //   })
  // }
    console.log('App Launch', opts)
    if (data && data.path) {
      wx.navigateTo({
        url: data.path,
      })
    }
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        env: config.envId,
        traceUser: true,
      })
    }
  },

  
  onShow(opts) {
    console.log('App Show', opts)
    // console.log(wx.getSystemInfoSync())
  },
  onHide() {
    console.log('App Hide')
  },
  onThemeChange({ theme }) {
    this.globalData.theme = theme
    themeListeners.forEach((listener) => {
        listener(theme)
    })
  },
  watchThemeChange(listener) {
      if (themeListeners.indexOf(listener) < 0) {
          themeListeners.push(listener)
      }
  },
  unWatchThemeChange(listener) {
      const index = themeListeners.indexOf(listener)
      if (index > -1) {
          themeListeners.splice(index, 1)
      }
  },
  globalData: {
    theme: wx.getSystemInfoSync().theme,
    hasLogin: false,
    openid: null,
    iconTabbar: '/page/weui/example/images/icon_tabbar.png',
  },
  // lazy loading openid
  getUserOpenId(callback) {
    const self = this

    if (self.globalData.openid) {
      callback(null, self.globalData.openid)
    } else {
      wx.login({
        success(data) {
          wx.cloud.callFunction({
            name: 'login',
            data: {
              action: 'openid'
            },
            success: res => {
              console.log('拉取openid成功', res)
              self.globalData.openid = res.result.openid
              callback(null, self.globalData.openid)
            },
            fail: err => {
              console.log('拉取用户openid失败，将无法正常使用开放接口等服务', res)
              callback(res)
            }
          })
        },
        fail(err) {
          console.log('wx.login 接口调用失败，将无法正常使用开放接口等服务', err)
          callback(err)
        }
      })
    }
  },
  // 通过云函数获取用户 openid，支持回调或 Promise
  getUserOpenIdViaCloud() {
    return wx.cloud.callFunction({
      name: 'wxContext',
      data: {}
    }).then(res => {
      this.globalData.openid = res.result.openid
      return res.result.openid
    })
  }
})
